<template>
  <div id="userLoginView" class="login-container">
    <!-- 顶部图标区域 -->

    <h2 class="login-title">用户登录</h2>

    <a-form
      class="login-form"
      label-align="left"
      auto-label-width
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>

      <a-form-item field="userPassword" tooltip="密码不少于 8 位" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>

      <a-form-item>
        <a-button
          class="round-rect-btn login-button"
          type="primary"
          html-type="submit"
        >
          登录
        </a-button>
      </a-form-item>

      <!-- 新增：注册链接 -->
      <div class="register-link-container">
        <span>还没有账号？</span>
        <router-link to="/user/register" class="register-link"
          >立即注册</router-link
        >
      </div>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);

const router = useRouter();
const store = useStore();

/**
 * 提交表单
 * @param data
 */
const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  // 登录成功，跳转到主页
  if (res.code === 0) {
    // 登录成功后立即存储用户信息
    console.log("登录返回数据:", res.data.data); // 检查这里的结构
    store.commit("user/setLoginUser", res.data.data);
    await store.dispatch("user/getLoginUser");

    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登陆失败，" + res.message);
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  box-sizing: border-box;
}

.login-icon-container {
  margin-bottom: 30px;
}

.login-icon {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
}

.login-title {
  margin: 0 0 30px 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.login-form {
  width: 100%;
  max-width: 480px;
  padding: 30px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 圆角矩形按钮样式 */
.round-rect-btn {
  border-radius: 8px; /* 中等圆角 */
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.login-button {
  width: 100%;
  min-width: 200px;
  height: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 按钮悬停效果 */
.round-rect-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

/* 注册链接样式 */
.register-link-container {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
}

.register-link {
  color: #165dff;
  text-decoration: none;
  margin-left: 4px;
}

.register-link:hover {
  text-decoration: underline;
}

/* 响应式适配 */
@media (max-width: 576px) {
  .login-form {
    padding: 20px;
  }

  .login-icon {
    width: 80px;
    height: 80px;
  }

  .login-title {
    font-size: 20px;
  }
}
</style>
